package main

import (
	"fmt"
	"net"
)

func main() {
	// 定义端口8888
	tcpAddr, _ := net.ResolveTCPAddr("tcp", ":8888")
	// 监听
	listener, _ := net.ListenTCP("tcp", tcpAddr)
	// 循环处理
	for {
		conn, err := listener.AcceptTCP()
		if err != nil {
			fmt.Println(err)
		}
		go handleConnection(conn)
	}
}

func handleConnection(conn *net.TCPConn) {
	for {
		but := make([]byte, 1024)
		_, err := conn.Read(but)
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Println(conn.RemoteAddr().String() + "  " + string(but))
		conn.Write([]byte("收到了：" + string(but)))
	}
}
